/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://jwsdp.dev.java.net/CDDLv1.0.html
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
 * add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your
 * own identifying information: Portions Copyright [yyyy]
 * [name of copyright owner]
 */
package com.probiz.estore.eai.common;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.core.util.GenericsUtils;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.eai.exception.EaiException;

/**
 * XML文件导出泛型基类
 * 
 * @see GenericPartialUnmarshaller
 * 
 * @author pengzhirong
 */
public abstract class GenericMarshaller<T>{
    
	protected final transient Log	logger	= LogFactory.getLog(getClass());
	
	protected Class<T> entitySetClass = null;
	
	private JAXBContext jaxbContext;
	
	protected EaiUtil eaiUtil = null;
	
	public GenericMarshaller(){
		entitySetClass = GenericsUtils.getSuperClassGenricType(getClass());
		
		try {
			jaxbContext = JAXBContext.newInstance(entitySetClass.getPackage().getName());
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取将要保存的XML文件全路径，即要导出的xml文件的全路径
	 * @return
	 */
	protected String getTargetXmlFilePath(){
		return eaiUtil.getTargetXmlFilePathByEntitySetClass(entitySetClass, true);
	}
	
	/**
	 * 构造XML的全路径，文件名带时间戳，如D:\saleorder_2009.09.17_15.58.23.xml
	 * @return
	 */
	private String getCurrentTargetXmlFilePath(){
		String fileNameTimePart = DateUtil.getDate(new Date(), "yyyy.MM.dd_HH.mm.ss");
		
		String curTargetXmlFilePath = getTargetXmlFilePath();
		int lastDotIndex = curTargetXmlFilePath.lastIndexOf(".");
		if(lastDotIndex!=-1)
			curTargetXmlFilePath = curTargetXmlFilePath.substring(0, lastDotIndex)+"_"+fileNameTimePart+curTargetXmlFilePath.substring(lastDotIndex, curTargetXmlFilePath.length());
		
		if(logger.isDebugEnabled())
			logger.debug("CurrentTargetXmlFilePath:" + curTargetXmlFilePath);
		
		return curTargetXmlFilePath;
	}
	
	/**
	 * 导出到xml文件
	 */
	public void startMarshal() throws EaiException{
		try{
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
			
			FileOutputStream fos = new FileOutputStream(getCurrentTargetXmlFilePath());
			
			marshaller.marshal(populateEntitySetElement(), fos);
			
		} catch (JAXBException e) {
			throw new EaiException(I18nUtil.getInstance().getMessage("eaiJobLog.exception.xml.marshal"), e);
		} catch (FileNotFoundException e){
			throw new EaiException(I18nUtil.getInstance().getMessage("eaiJobLog.exception.fileNotFound"), e);
		}

			
	}
	
	/**
	 * 装配业务实体集合数据
	 * @return
	 * @throws Exception
	 */
	public abstract JAXBElement<T> populateEntitySetElement() throws EaiException;

	public void setEaiUtil(EaiUtil eaiUtil) {
		this.eaiUtil = eaiUtil;
	}
	
}
